﻿package util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

//字符串工具类
public class StringUtil {
	// 判断字符串是否为空
	public static boolean isNull(String value) {
		if ("".equals(value) || null == value || value.equalsIgnoreCase("null")
				|| "undefined".equals(value)) {
			return true;
		}
		return false;
	}
	
	public static String getTableName(String tableName) {
		return tableName.toLowerCase();
	}
	
	public static String getMoreIdCond(String ids) {
		String[] idArr = ids.split(",");
		return getMoreIdCond(idArr);
	}
	public static String getMoreIdCond(String[] idArr) {
		String idCon = "";
		int size = idArr.length;
		for(int i = 0;i<size;i++) {
			idCon += ",'" + idArr[i] + "'"; 
		}
		if(!"".equals(idCon)) {
			idCon = idCon.substring(1);
		}
		return idCon;
	}

	// 处理路径,统一将斜杠变为"/"
	public static String dealPath(String path) {
		String ret = "";
		for (int i = 0; i < path.length(); i++) {
			char c = path.charAt(i);
			if (c == '\\') {
				ret += '/';
			} else {
				ret += c;
			}
		}

		// 最后以"/"结束
		if (ret.charAt(ret.length() - 1) != '/') {
			ret += '/';
		}
		return ret;
	}

	// 去掉换行符号，java->js
	public static String removeLineBreaks(String str) {
		return str.replaceAll("\r\n", " ");
	}

	/**
	 * 将一串字符转换成HTML能使用的字符串,可用于csv绑定。
	 * <p>
	 * 详细
	 * 
	 * @param java
	 *            .lang.String 字符串
	 * @return java.lang.String 字符串
	 */
	public static String string2Html(String preStr) throws java.lang.Exception {

		if (preStr == null)
			return "";
		preStr = preStr.trim();

		StringBuffer strB = new StringBuffer();

		for (int j = 0; j < preStr.length(); j++) {
			if (preStr.charAt(j) == '\r')
				strB.append("");
			// strB.append("<br>");
			/*
			 * else if (preStr.charAt(j) == '\n') strB.append("<br>");
			 */
			else if (preStr.charAt(j) == '\'')
				strB.append("&#39;");
			else if (preStr.charAt(j) == '"')
				strB.append("&quot;");
			else if (preStr.charAt(j) == '\t')
				strB.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			/*
			 * else if (preStr.charAt(j) == ' ') strB.append("&nbsp;");
			 */
			else if (preStr.charAt(j) == '>')
				strB.append("&gt;");
			else if (preStr.charAt(j) == '<')
				strB.append("&lt;");
			else if (preStr.charAt(j) == '&')
				strB.append("&amp;");
			else if (preStr.charAt(j) == '\\')
				strB.append("&#92;");
			else if (preStr.charAt(j) == ',')
				strB.append("&#44;");
			else
				strB.append(preStr.charAt(j));
		}

		return strB.toString();
	}

	/**
	 * 将一串字符转换成脚本串变量能使用的字符串。
	 * <p>
	 * 详细
	 * 
	 * @param java
	 *            .lang.String 字符串
	 * @return java.lang.String 字符串
	 */
	public static String string2Script(String preStr) {

		if (preStr == null)
			return "";
		// preStr = preStr.trim();

		StringBuffer strB = new StringBuffer();
		int len = preStr.length();
		for (int j = 0; j < len; j++) {
			if (preStr.charAt(j) == '\n')
				strB.append('\\').append('n');

			else if (preStr.charAt(j) == '\'')
				strB.append('\\').append('\'');

			else if (preStr.charAt(j) == '"')
				strB.append('\\').append('"');

			else if (preStr.charAt(j) == '\t')
				strB.append('\\').append('t');

			else if (preStr.charAt(j) == '\r')
				strB.append('\\').append('r');

			else if (preStr.charAt(j) == '\\') {
				if (j < len - 1
						&& (preStr.charAt(j + 1) == 'n'// 很可能原串的本意就是要转义处理，故保留。
								|| preStr.charAt(j + 1) == 'r' || preStr
								.charAt(j + 1) == 't'))
					strB.append(preStr.charAt(j));
				else
					strB.append('\\').append('\\');

			} else if (preStr.charAt(j) == '/')
				strB.append('\\').append('/');

			else
				strB.append(preStr.charAt(j));

		}

		return strB.toString();
	}

	public static boolean isMatche(String str, String filterStr) {
		filterStr = filterStr.replace('.', '#');
		filterStr = filterStr.replaceAll("#", "\\\\.");
		filterStr = filterStr.replace('*', '#');
		filterStr = filterStr.replaceAll("#", ".*");
		filterStr = filterStr.replace('?', '#');
		filterStr = filterStr.replaceAll("#", ".?");
		filterStr = "^" + filterStr + "$";

		Pattern p = Pattern.compile(filterStr);
		Matcher fMatcher = p.matcher(str);
		return fMatcher.matches();
	}

}
